package com.module_live.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.herewhite.sdk.AbstractRoomCallbacks;
import com.herewhite.sdk.Room;
import com.herewhite.sdk.RoomParams;
import com.herewhite.sdk.WhiteBroadView;
import com.herewhite.sdk.WhiteSdk;
import com.herewhite.sdk.WhiteSdkConfiguration;
import com.herewhite.sdk.domain.AnimationMode;
import com.herewhite.sdk.domain.Appliance;
import com.herewhite.sdk.domain.DeviceType;
import com.herewhite.sdk.domain.EventEntry;
import com.herewhite.sdk.domain.EventListener;
import com.herewhite.sdk.domain.MemberState;
import com.herewhite.sdk.domain.Promise;
import com.herewhite.sdk.domain.RoomState;
import com.herewhite.sdk.domain.SDKError;
import com.herewhite.sdk.domain.UrlInterrupter;
import com.lib.fastkit.utils.log.LogUtil;
import com.module_live.utils.white_board.DemoAPI;

public class WhiteBoardUtils {

    /*和 iOS 名字一致*/
    public static final String EVENT_NAME = "WHITECOMMANDCUSTOMEVENTLRLIVEATACONTROLLER";

    private DemoAPI demoAPI;
    private Gson gson;
    private Room room;

    private WhiteBroadView whiteBroadView;

    private Activity activity;

    private static WhiteBoardUtils instance;

    public WhiteBoardUtils() {

        demoAPI = new DemoAPI();
        gson = new Gson();
    }

    public static synchronized WhiteBoardUtils getInstance() {

        if (instance == null) {
            instance = new WhiteBoardUtils();
        }
        return instance;
    }


    public WhiteBoardUtils joinToRoom(Activity activity, WhiteBroadView broadView, String uuid,
                                      String token, String userPhone) {
        this.activity = activity;
        this.whiteBroadView = broadView;

        joinRoom(uuid, token, userPhone);

        return instance;

    }

    private void joinRoom(String uuid, String roomToken, String userPhone) {

        WhiteSdkConfiguration sdkConfiguration = new WhiteSdkConfiguration(DeviceType.touch, 10, 0.1, true);
        /*显示用户头像*/
        sdkConfiguration.setUserCursor(true);
        /*接受用户头像信息回调，自己实现头像回调。会导致 UserCursor 设置失效。*/
        WhiteSdk whiteSdk = new WhiteSdk(
                whiteBroadView,
                activity,
                sdkConfiguration,
                new UrlInterrupter() {
                    @Override
                    public String urlInterrupter(String sourceUrl) {
                        return sourceUrl;
                    }
                });

        whiteSdk.joinRoom(new RoomParams(uuid, roomToken, userPhone), new AbstractRoomCallbacks() {

            @Override
            public void onKickedWithReason(String reason) {
                LogUtil.e("@@@@@@@@@@@@@@@@" + reason);

                if (listener != null) {
                    listener.onKickedOutRoom(reason);
                }
            }

            @Override
            public void onDisconnectWithError(Exception e) {
                super.onDisconnectWithError(e);

                LogUtil.e("断开后，尝试重新连接");
                if (listener != null) {
                    listener.onRetryJoinin();
                }
            }

            @Override
            public void onRoomStateChanged(RoomState modifyState) {

                showLog("onRoomStateChanged:" + gson.toJson(modifyState));

                if (listener != null) {
                    listener.onRoomStateChange();
                }

            }
        }, new Promise<Room>() {
            @Override
            public void then(Room wRoom) {

                showLog("加入画板房间成功!");

                room = wRoom;
                setTool(room);
                if (listener != null) {
                    listener.onJoinRoomSucess(room);
                }

                room.scalePptToFit(AnimationMode.Continuous);

            }

            @Override
            public void catchEx(SDKError t) {
                showLog("加入白板直播间失败，原因是：" + t.getMessage());
            }
        });


    }

    private void addCustomEventListener() {

        room.addMagixEventListener(EVENT_NAME, new EventListener() {
            @Override
            public void onEvent(EventEntry eventEntry) {
                showLog("customEvent payload: " + eventEntry.getPayload().toString());

            }
        });
    }

    /**
     * Log
     *
     * @param msg
     */
    public static void showLog(final String msg) {

        Log.e("调试日志======", msg);
    }

    public void setTool(Room room) {
        MemberState memberState = new MemberState();
//        memberState.setStrokeColor(ColorUtil.int2Rgb(activity.getResources().getColor(R.color.c1)));
        memberState.setCurrentApplianceName(Appliance.PENCIL);
        memberState.setStrokeWidth(1);
        memberState.setTextSize(5);

        if (room != null) {
            room.setMemberState(memberState);
        }

    }


    /**
     * Toast
     *
     * @param text
     */

    private Toast mToast;

    public void showToast(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (mToast == null) {
                mToast = Toast.makeText(activity, "", Toast.LENGTH_SHORT);
            }
            mToast.setText(text);
            mToast.show();
        }
    }


    public interface WhiteBoardListener {

        void onJoinRoomSucess(Room room);

        void onRoomStateChange();

        void onRetryJoinin();

        void onKickedOutRoom(String reason);

    }

    private WhiteBoardListener listener;

    public void setWhiteBoardListener(WhiteBoardListener whiteBoardListener) {

        this.listener = whiteBoardListener;

    }

}
